<?php

namespace App\Http\Resources\Api;

use App\Models\GroupCourse;
use App\Models\Tenant;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class GroupCourseOrderResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $tenant = Tenant::find($this->tenant_id);
        $group_course_cover_url = config('app.url') . "uploads/{$tenant->admin_prefix}/" . $this->groupCourse->cover_url;
        $coach_cover_url = config('app.url') . "uploads/{$tenant->admin_prefix}/" . $this->groupCourse->coach->cover_url;
        $full_course_time = $this->course_date . ' ' . GroupCourse::WEEKS[$this->groupCourse->week] . ' ' . $this->course_start_time . '~' . $this->course_end_time;
        $qrcode_field = config("{$tenant->admin_prefix}.custom.group_course_order.weapp.audit_qrcode_field");
        $qrcode = $this->$qrcode_field ? asset($this->$qrcode_field) : '';

        return [
            'id' => $this->id,
            'group_course_title' => $this->groupCourse->title,
            'group_course_cover_url' => $group_course_cover_url,
            'group_course_intro' => Str::limit($this->groupCourse->intro, 50, '...'),
            'coach_cover_url' => $coach_cover_url,
            'coach_name' => $this->groupCourse->coach->realname,
            'coach_brief_intro' => $this->groupCourse->coach->brief_intro,
            'full_course_time' => $full_course_time,
            'shop_address' => $this->tenant->address,
            'qrcode_url' => $qrcode,
        ];
    }
}
